package com.yudaob2b.business.utils.aliyun.mq;

import com.aliyun.openservices.ons.api.*;
import com.yudaob2b.business.constant.AliyunConstant;

import java.util.Properties;

/**
 * 阿里云mq工具类
 *
 * @Author: Kidd.Tan
 * @Description
 * @Date: Create in 16:34 2018/7/5
 */
public class CloudMQUtil {

    /**
     * 调用mq发送代码
     *
     *  Producer producer = CloudMQUtil.getProducer(AliyunConstant.PID_ELONGHOTEL);
     *  Message msg = new Message(AliyunConstant.TOPIC_NAME, "tag_003", "key_003", "这是消息".getBytes());
     *  producer.send(msg);
     */

    /**
     * 获取消息的 Producer
     *
     * @param producerId producerId
     * @return Producer
     */
    public static Producer getProducer(String producerId) {
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.ProducerId, producerId);
        properties.put(PropertyKeyConst.AccessKey, AliyunConstant.ACCESS_KEY);
        properties.put(PropertyKeyConst.SecretKey, AliyunConstant.SECRET_KEY);
        Producer producer = ONSFactory.createProducer(properties);

        // 在发送消息前，必须调用start方法来启动Producer，只需调用一次即可。
        producer.start();
        return producer;
    }



}
